<!-- 新增/编辑指标 -->
<template>
  <a-drawer
    width="45%"
    :visible="props.visible"
    @ok="handleOk"
    @cancel="handleCancel"
    draggable
    title-align="center"
    unmountOnClose
  >
    <template #title> {{ props.data?.id ? '编辑指标' : '新增指标' }} </template>
    <div>
      <a-form :model="formData" ref="refForm" :rules="rules" auto-label-width>
        <a-form-item label="工作类型" field="type" show-colon>
          <a-select v-model="formData.type">
            <a-option :value="1">重点工作</a-option>
            <a-option :value="2">例行工作</a-option>
            <a-option :value="3">新增工作</a-option>
          </a-select>
        </a-form-item>
        <a-form-item label="工作计划项目" field="name" show-colon>
          <a-input v-model="formData.name"></a-input>
        </a-form-item>
        <a-form-item label="描述" field="define" show-colon>
          <a-textarea
            v-model="formData.define"
            show-word-limit
            :max-length="50"
            :auto-size="{
              minRows: 4
            }"
          ></a-textarea>
        </a-form-item>
        <a-form-item label="目标及要求" field="definition" show-colon>
          <a-textarea
            v-model="formData.definition"
            show-word-limit
            :max-length="50"
            :auto-size="{
              minRows: 4
            }"
          ></a-textarea>
        </a-form-item>
        <a-form-item label="规则" field="rule" show-colon>
          <a-textarea
            v-model="formData.rule"
            show-word-limit
            :max-length="50"
            :auto-size="{
              minRows: 4
            }"
          ></a-textarea>
        </a-form-item>
        <a-form-item label="指标权重" field="indexWeight" show-colon>
          <a-input-number v-model="formData.indexWeight" hide-button>
            <template #suffix>%</template></a-input-number
          >
        </a-form-item>
      </a-form>
    </div>
    <template #footer>
      <a-space>
        <a-button @click="handleCancel">取消</a-button>
        <a-button type="primary" @click="handleOk">确认</a-button>
        <a-button v-if="!formData?.id" type="primary" @click="continueAdd"
          >确认并继续新增</a-button
        >
      </a-space>
    </template>
  </a-drawer>
</template>

<script lang="ts" setup>
import { cloneDeep } from 'lodash';
import { PerformanceKeyPerformanceIndexLibraryUse } from '@/apis/management/performance/baseSetting/template/type';
import { GetPpiIndexRepeat } from '@/apis/management/performance/baseSetting/template/index';
import { useVModel } from '@vueuse/core';
const props = withDefaults(
  defineProps<{
    visible: boolean; // 是否显示对话框
    data?: PerformanceKeyPerformanceIndexLibraryUse; // 传进来的数据,
  }>(),
  {}
);
const emits = defineEmits(['update:visible', 'submit', 'exist']);
let refForm = ref();
const formData = useVModel(props, 'data', emits);
/**
 * 查重
 */
const validateName = async (value: any, cb: any) => {
  //排除本来的名称
  if (value === formData.value?.oldName) return;
  // 排查即将新增的数据
  emits('exist', formData.value, (isExist: boolean) => {
    if (isExist) {
      cb('名称已存在!');
      return;
    }
  });
  // 排查数据库的数据
  // const res = await GetPpiIndexRepeat({
  //   name: value
  // });
  // if (res?.code === 400) {
  //   cb('名称已存在!');
  // }
};
const rules = reactive({
  name: [
    {
      required: true,
      message: '请输入'
    },
    {
      validator: validateName
    }
  ],
  indexWeight: [
    {
      required: true,
      message: '请输入'
    }
  ],
  rules: [
    {
      required: true,
      message: '请输入'
    }
  ],
  type: [
    {
      required: true,
      message: '请选择'
    }
  ]
});

const handleOk = async () => {
  if (await refForm.value.validate()) {
    return;
  }
  emits('submit', formData.value);
  emits('update:visible', false);
};

const handleCancel = () => {
  emits('update:visible', false);
};
const continueAdd = async () => {
  if (await refForm.value.validate()) {
    return;
  }
  emits('submit', formData.value);
};
</script>

<style scoped lang="less">
.icon {
  cursor: pointer;
  font-size: 20px;
}
</style>
